/**
 * 
 */
package action.student;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;


import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import model.*;
/** 
 * @作者 Zouzr 
 * @创建日期 Dec 19, 2008 
 * @版本 V 1.0 
 */

public class LoginAction extends ActionSupport 
{
	
	private String stunumber;//
	
	private String stupassword;
	
	private String checkcode;
	
	private String message="";
	
	
	/**
	 * 修改密码的属性
	 */
	private String stupasswordold;//旧密码
	
	private String stupasswordnew;//新密码
	
	private String stupasswordchecknew;//确认新密码
	
	private ServletActionContext context;
	
	/**
	 * 登陆验证
	 * @return
	 */
	public String studentLogin()
	{
		if(stunumber.equals("")||stunumber==null)
		{
			message=message+"用户名未填写<br>";
			return SUCCESS;
		}
		if(stupassword.equals("")||stupassword==null)
		{
			message=message+"密码未填写<br>";
			return SUCCESS;
		}
		if(checkcode.equals("")||checkcode==null)
		{
			message=message+"验证码未填写<br>";
			return SUCCESS;
		}
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDAO studentdao = (StudentDAO)context.getBean("StudentDAOProxy");
        String result=studentdao.loginValidate(stunumber, stupassword);
        if(result.equals("UserNotExist"))
        {
        	message=message+"用户不存在<br>";
			return SUCCESS;
        }
        if(result.equals("PasswordError"))
        {
        	message=message+"密码错误<br>";
			return SUCCESS;
        }
        Map map = ActionContext.getContext().getSession();
        String temp_checkcode = (String) map.get("randCode");
        if(!checkcode.equals(temp_checkcode))
		{
			message=message+"验证码错误<br>";
			return SUCCESS;
		}
        System.out.println("result---------"+result);
        
        String[] results = result.split(";");
        this.context.getRequest().getSession().setAttribute("stuId", results[0]);
        this.context.getRequest().getSession().setAttribute("stunumber", stunumber);
        this.context.getRequest().getSession().setAttribute("stuname", results[1]);
        System.out.println("----------"+ this.context.getRequest().getSession().getAttribute("stunumber"));
        return SUCCESS;
	}

	/**
	 * 退出
	 * @return
	 */
	public String studentLogout()
	{
		this.context.getRequest().getSession().setAttribute("stuId", "");
		this.context.getRequest().getSession().setAttribute("stunumber", "");
        this.context.getRequest().getSession().setAttribute("stuname", "");
        System.out.println("退出后----------"+ this.context.getRequest().getSession().getAttribute("stunumber"));
        System.out.println("退出后----------"+ this.context.getRequest().getSession().getAttribute("stuname"));
		return SUCCESS;
	}
	/**
	 * 修改密码
	 */
	public String studentPasswordEdit()
	{
		if(stupasswordold.equals("")||stupasswordold==null)
		{
			message=message+"旧密码未填写<br>";
			return SUCCESS;
		}
		if(stupasswordnew.equals("")||stupasswordnew==null)
		{
			message=message+"新密码未填写<br>";
			return SUCCESS;
		}
		if(stupasswordchecknew.equals("")||stupasswordchecknew==null)
		{
			message=message+"确认新密码未填写<br>";
			return SUCCESS;
		}
		if(checkcode.equals("")||checkcode==null)
		{
			message=message+"验证码未填写<br>";
			return SUCCESS;
		}
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDAO studentdao = (StudentDAO)context.getBean("StudentDAOProxy");
        System.out.println("修改密码时的用户名为"+this.context.getRequest().getSession().getAttribute("stunumber"));
        String result=studentdao.loginValidate((String)this.context.getRequest().getSession().getAttribute("stunumber"), stupasswordold);
        if(result.equals("PasswordError"))
        {
        	message=message+"密码错误<br>";
			return SUCCESS;
        }
        if(!stupasswordnew.equals(stupasswordchecknew))
        {
        	message=message+"新密码和确认密码必须一致<br>";
			return SUCCESS;
        }
        Map map = ActionContext.getContext().getSession();
        String temp_checkcode = (String) map.get("randCode");
        if(!checkcode.equals(temp_checkcode))
		{
			message=message+"验证码错误<br>";
			return SUCCESS;
		}
        studentdao.editPassword((String)this.context.getRequest().getSession().getAttribute("stunumber"), stupasswordnew);
		return SUCCESS;
	}

	/**
	 * @return the stunumber
	 */
	public String getStunumber() {
		return stunumber;
	}

	/**
	 * @param stunumber the stunumber to set
	 */
	public void setStunumber(String stunumber) {
		this.stunumber = stunumber;
	}

	/**
	 * @return the stupassword
	 */
	public String getStupassword() {
		return stupassword;
	}

	/**
	 * @param stupassword the stupassword to set
	 */
	public void setStupassword(String stupassword) {
		this.stupassword = stupassword;
	}

	/**
	 * @return the checkcode
	 */
	public String getCheckcode() {
		return checkcode;
	}

	/**
	 * @param checkcode the checkcode to set
	 */
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the stupasswordold
	 */
	public String getStupasswordold() {
		return stupasswordold;
	}

	/**
	 * @param stupasswordold the stupasswordold to set
	 */
	public void setStupasswordold(String stupasswordold) {
		this.stupasswordold = stupasswordold;
	}

	/**
	 * @return the stupasswordnew
	 */
	public String getStupasswordnew() {
		return stupasswordnew;
	}

	/**
	 * @param stupasswordnew the stupasswordnew to set
	 */
	public void setStupasswordnew(String stupasswordnew) {
		this.stupasswordnew = stupasswordnew;
	}

	/**
	 * @return the stupasswordchecknew
	 */
	public String getStupasswordchecknew() {
		return stupasswordchecknew;
	}

	/**
	 * @param stupasswordchecknew the stupasswordchecknew to set
	 */
	public void setStupasswordchecknew(String stupasswordchecknew) {
		this.stupasswordchecknew = stupasswordchecknew;
	}
	
	
}
